<?php

/**
 * @author	barbarosalcin
 * @desc 	inc.init / for video blog systems. check url description below
 * @package	core
 * @version	$Id: 20110124
 */

//
// addslashes to vars if magic_quotes_gpc is off


if (! get_magic_quotes_gpc())
{
	if (is_array($_GET))
	{
		while(list ( $k, $v ) = each($_GET))
		{
			if (is_array($_GET[$k]))
			{
				while(list ( $k2, $v2 ) = each($_GET[$k]))
				{
					$_GET[$k][$k2] = addslashes($v2);
				}
				@reset($_GET[$k]);
			}
			else
			{
				$_GET[$k] = addslashes($v);
			}
		}
		@reset($_GET);
	}
	
	if (is_array($_POST))
	{
		while(list ( $k, $v ) = each($_POST))
		{
			if (is_array($_POST[$k]))
			{
				while(list ( $k2, $v2 ) = each($_POST[$k]))
				{
					$_POST[$k][$k2] = addslashes($v2);
				}
				@reset($_POST[$k]);
			}
			else
			{
				$_POST[$k] = addslashes($v);
			}
		}
		@reset($_POST);
	}
	
	if (is_array($_COOKIE))
	{
		while(list ( $k, $v ) = each($_COOKIE))
		{
			if (is_array($_COOKIE[$k]))
			{
				while(list ( $k2, $v2 ) = each($_COOKIE[$k]))
				{
					$_COOKIE[$k][$k2] = addslashes($v2);
				}
				@reset($_COOKIE[$k]);
			}
			else
			{
				$_COOKIE[$k] = addslashes($v);
			}
		}
		@reset($_COOKIE);
	}
}

date_default_timezone_set('Europe/Moscow');
//set_time_limit(0);
setlocale(LC_ALL, explode(',',$config["locale"]));
// Foolproof.
setlocale(LC_CTYPE, 'C');

/**
 * @author barbarosalcin
 * @desc url handling part 
 * @example [http://www.address.com/SUBPAGE/] =
 * @example [http://www.address.com/SUBPAGE/la-index.html]
 * @example [http://www.address.com/aaa/bbb/la-index-CATEGORY-SUBCATEGORY-ID.html] =
 * @example [http://www.address.com/aaa/la-index-CATEGORY-SUBCATEGORY-ID.html]
 * @example [http://www.address.com/SUBPAGE/] =
 * @example [http://www.address.com/SUBPAGE/la-index.html]
 */

$regex = '%(?P<domain>[a-z0-9-.]+.[a-z0-9]|[a-z0-9]+)/(?P<request>[\w./_-]+)%i';

$req = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

$req = preg_replace('!(.+)/' . substr($config['docroot'], 1, - 1) . '/(.+)!', "$1/$2", $req);

preg_match_all($regex, $req, $response, PREG_PATTERN_ORDER);

$req = array(
		'domain' => $response['domain'][0], 
		'request' => explode('/', preg_replace('%^(.+)/$%i',"$1",$response['request'][0])), 
		'uri' => $response['request'][0], 
		'get' => $_GET);

$req['subpage_orig'] = $req['request'][0];
$req['subpage'] = str_replace('-', '_', $req['request'][0]);

$req['ajax'] = ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')) || ((int) $req['get']['jx'] || (int) $_POST['jx'])) ? 1 : 0;

$req['end'] = end($req['request']);

if (preg_match('%^(?P<language>\w{2})-\w+.\w{3,4}$%i', $req['end'], $regs))
{
	$req['language'] = $regs['language'];
}

if (preg_match('%^admin/\w{2}-(?P<action>[\w_-]+).html%i', $req['uri'], $regs))
{
	$req['action'] = $regs['action'];
}

unset($regs);
unset($response);
$lang = array();

/* language stuff */

$req['language'] = (is_dir(__PHPROOT__.'lang'.DS . $req['language']) && $req['language']) ? $req['language'] : $config['default_language'];
$req['default_index'] = $req['language'] . '-index' . $config['seoext'];

/* ip thingy */

$client_ip = (! empty($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : ((! empty($_ENV['REMOTE_ADDR'])) ? $_ENV['REMOTE_ADDR'] : getenv('REMOTE_ADDR'));

if (is_dir(__PHPROOT__.'lang' .DS. $req['language']) && ($handle = opendir(__PHPROOT__.'lang' .DS. $req['language'])))
{
	while(false !== ($file = readdir($handle)))
	{
		if ($file != "." && $file != ".." && preg_match('/lang_.+\.php/i', $file))
		{
			include (__PHPROOT__.'lang' .DS. $req['language'] . DS . $file);
		}
	}
	closedir($handle);
}
